﻿
namespace Leetcode.N1703;
public class Solution
{
    public int MinMoves(int[] nums, int k)
    {
        var count = 0;
        var sums = new List<int>();
        sums.Add(0);
        for (int i = 0; i < nums.Length; i++)
        {
            if (nums[i] == 1)
            {
                count++;
                sums.Add((i + 1) - count + sums[sums.Count - 1]);
            }
        }

        var minMoves = int.MaxValue;
        for (int i = 1; i <= sums.Count - k; i++)
        {
            var moves = (sums[i + k - 1] - sums[i + k - 1 - k / 2]) - (sums[i - 1 + k / 2] - sums[i - 1]);
            minMoves = Math.Min(minMoves, moves);
        }

        return minMoves;
    }
}